package net.child.chess.pieces;

import java.util.List;
import net.child.chess.R;
import net.child.chess.utils.ChessDef;
import net.child.chess.utils.Position;

public class Knight extends Piece
{
	public Knight(ChessDef.Color color, int position)
	{
		super(color, Piece.Type.KNIGHT, initialState(position, color), imageId(color));		
	}
	
	@Override
	public void attacks(List<Integer> squares, IMoves iMoves)
	{
		Position toPosition = new Position(position());
		toPosition.position(toPosition.x() - 2, toPosition.y() - 1);
		if(toPosition.isValid())
		{
			addAttackIfValid(squares, toPosition.position(), iMoves);
		}
		toPosition.position(position());
		toPosition.position(toPosition.x() - 2, toPosition.y() + 1);
		if(toPosition.isValid())
		{
			addAttackIfValid(squares, toPosition.position(), iMoves);
		}
		
		toPosition.position(position());
		toPosition.position(toPosition.x() + 2, toPosition.y() - 1);
		if(toPosition.isValid())
		{
			addAttackIfValid(squares, toPosition.position(), iMoves);
		}
		toPosition.position(position());
		toPosition.position(toPosition.x() + 2, toPosition.y() + 1);
		if(toPosition.isValid())
		{
			addAttackIfValid(squares, toPosition.position(), iMoves);
		}
		
		toPosition.position(position());
		toPosition.position(toPosition.x() + 1, toPosition.y() + 2);
		if(toPosition.isValid())
		{
			addAttackIfValid(squares, toPosition.position(), iMoves);
		}
		toPosition.position(position());
		toPosition.position(toPosition.x() - 1, toPosition.y() + 2);
		if(toPosition.isValid())
		{
			addAttackIfValid(squares, toPosition.position(), iMoves);
		}
		
		toPosition.position(position());
		toPosition.position(toPosition.x() + 1, toPosition.y() - 2);
		if(toPosition.isValid())
		{
			addAttackIfValid(squares, toPosition.position(), iMoves);
		}
		toPosition.position(position());
		toPosition.position(toPosition.x() - 1, toPosition.y() - 2);
		if(toPosition.isValid())
		{
			addAttackIfValid(squares, toPosition.position(), iMoves);
		}
	}
	
	private static int imageId(ChessDef.Color color)
	{
		if(color == ChessDef.Color.BLACK)
		{
			return R.drawable.ic_black_knight;
		}
		else
		{
			return R.drawable.ic_white_knight;
		}
	}
	
	static private PieceState initialState(int position, ChessDef.Color color)
	{
		Position pos = new Position(position); 
		if(color == ChessDef.Color.BLACK)
		{
			return new PieceState(pos, (pos.y() != 0) || ((pos.x() != 1) && (pos.x() != 6)));
		}
		else
		{
			return new PieceState(pos, (pos.y() != 7) || ((pos.x() != 1) && (pos.x() != 6)));
		}
	}
}

